package com.alstudio.view.dialog;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.view.listener.AlItemOnClickListener;
import com.alstudio.view.listener.AlMultiChoiceOnClickListener;
import com.alstudio.view.listener.AlDialogOnClickListener;
import com.alstudio.view.listener.AlertDialogEventListener;
import com.alstudio.view.widget.AlButton;
import com.loovee.imaohu.R;

/**
 * 此类封装系统对话框，控制输出系统对话框或者自定义对话框
 * 
 * @author Alonso Lee
 */

public class AlertDialogCreator implements Serializable {

	private static final long serialVersionUID = -7471331418217585598L;

	// 设定是否创建系统对话框
	private boolean isSystemDialog = true;

	private boolean isNeedIcon = false;
	// 缺省不能关闭对话框
	private boolean isCancelable = true;
	private Context mContext;
	// 左边按钮名
	private String leftButtonName;
	// 右边按钮名
	private String rightButtonName;
	// 中间按钮名
	private String centralButtonName;
	// 标题栏文字
	private String title = "";
	// 消息栏文字
	private String message = "";
	// 进度说明消息
	private String progressMessage;
	// 自定义view
	private View customContentView = null;

	// 用于监听系统对话框确定键的按下事件
	private DialogInterface.OnClickListener postiveListener;
	// 用于监听系统对话框取消键的按下事件
	private DialogInterface.OnClickListener negativeListener;
	// 用于监听系统对话框中间键的按下事件
	private DialogInterface.OnClickListener neutralListener;
	// 用于监听系统对话框对话框item项被按下的事件
	private DialogInterface.OnClickListener itemListener;
	// 用于监听系统对话框的单选按下事件
	private DialogInterface.OnClickListener singleChoiceListener;
	// 用于监听系统对话框的多按下事件
	private DialogInterface.OnMultiChoiceClickListener multiChoiceListener;

	// 用于监听自定义对话框中间键按下事件
	private OnItemClickListener cItemClickListener;
	// 用于监听自定义对话框中间键按下事件
	private OnItemClickListener cSingleChoickClickListener;
	// 用于监听自定义对话框中间键按下事件
	private OnItemClickListener cMultiChoiceClickListener;

	// 回调通知UI，左键按下
	private AlDialogOnClickListener mLeftKeyListener;
	// 回调通知UI,右键按下
	private AlDialogOnClickListener mRightKeyListener;
	// 回调通知UI，中间件按下
	private AlDialogOnClickListener mCentralKeyListener;
	// 回调通知UI，对话框item选项按下
	private AlItemOnClickListener mItemClickListener;
	// 回调通知UI，单选对话框某item按下
	private AlItemOnClickListener mSingleChoiceListener;
	// 回调通知UI,多选对话框某item按下
	private AlMultiChoiceOnClickListener mMultiChoiceListener;

	// 列表对话框数据源
	private String[] items;
	// 单选对话框数据源
	private String[] singleChoiceItems;
	// 多选对话框数据源
	private String[] multiChoiceItems;
	// 多选对话框缺省状态数据源
	private boolean[] multiCheckedItem;
	// 多选数据源
	private CheckableItem[] mMultiChoiceItems;

	// 单选对话框选中项
	private int checkedItem;
	// 自定义图片
	private Bitmap mBitmap;
	// 左键自定义资源文件
	private int leftButtonSelector = -1;
	// 右键自定义资源文件
	private int rightButtonSelector = -1;
	// 中间键自定义资源文件
	private int centralButtonSelector = -1;
	// 对话框对象
	private Dialog dialog;

	private TextView tvmsg = null, tvtitle = null, tvProgressMessage;
	//
	private ImageView mIcon = null;
	// 用于生成自定义列表对话框
	private ListView mListView;

	// 自定义对话框左边按钮对象
	private AlButton mCustomleftButton;
	// 自定义对话框左边按钮对象
	private AlButton mCustomRightButton;
	// 自定义对话框左边按钮对象
	private AlButton mCustomCentralButton;
	// 单选列表当前选中项
	private int singleChoiceCurSelect = 0;
	// 此变量用于单选、多选对话框，控制
	private boolean isNeedshowBox = false;
	// 当前显示的是否是单选列表对话框
	private boolean isSingleChoice = false;

	// 创建的对话框模式
	private DialogStyle mStyle;

	// 当前进度
	private int mProgress = 0;
	// 进度条步进量
	private int incrementStep = 1;
	// 缺省进度条最大值
	private final int MAX_PROGRESS = 100;
	// 生成系统进度对话框对象
	private AlProgressDialog mProgressDialog;
	// UI监听对话框事件监听器
	private AlertDialogEventListener mAlertDialogEventsListener;
	// 用于生成自定义的列表对话框、单选对话框、多选对话框
	private ListItemAdapter mListItemAdapter;

	/**
	 * 构造带有自定义view的对话框
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param isNeedIcon
	 * @param title
	 * @param message
	 * @param mContentView
	 */
	public AlertDialogCreator(Context ctx, boolean isSystemDialog,
			boolean isNeedIcon, String title, String message, View mContentView) {
		this.mContext = ctx;
		this.isSystemDialog = isSystemDialog;
		this.title = title;
		this.message = message;
		this.isNeedIcon = isNeedIcon;
		this.customContentView = mContentView;
	}

	public AlertDialogCreator(Context ctx, boolean isSystemDialog,
			boolean isNeedIcon, String title, String message) {
		this.mContext = ctx;
		this.isSystemDialog = isSystemDialog;
		this.title = title;
		this.message = message;
		this.isNeedIcon = isNeedIcon;
	}

	public AlertDialogCreator(Context ctx, boolean isSystemDialog,
			DialogStyle style, String title, String message) {
		this.mContext = ctx;
		this.isSystemDialog = isSystemDialog;
		this.title = title;
		this.message = message;
		mStyle = style;
	}

	public AlertDialogCreator(Context ctx, boolean isSystemDialog,
			DialogStyle style, String title, String message, View customView) {
		this.mContext = ctx;
		this.isSystemDialog = isSystemDialog;
		this.title = title;
		this.message = message;
		mStyle = style;
		this.customContentView = customView;
	}

	public AlertDialogCreator(Context ctx, boolean isSystemDialog,
			DialogStyle style, String title, String[] items) {
		this.mContext = ctx;
		this.isSystemDialog = isSystemDialog;
		this.title = title;
		this.items = items;
		mStyle = style;
	}

	public AlertDialogCreator(Context ctx, boolean isSystemDialog,
			DialogStyle style, String title, String[] items,
			boolean[] checkedItem) {
		this.mContext = ctx;
		this.isSystemDialog = isSystemDialog;
		this.title = title;
		this.multiChoiceItems = items;
		mStyle = style;
		multiCheckedItem = checkedItem;
	}

	/**
	 * 
	 * 构造单选对话框
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param isNeedIcon
	 * @param title
	 * @param singleChoiceItems
	 * @param checkedItem
	 *            缺省选中项
	 * @param listener
	 */
	public AlertDialogCreator(Context ctx, boolean isSystemDialog,
			DialogStyle style, String title, String[] singleChoiceItems,
			int checkedItem) {
		this.mContext = ctx;
		this.isSystemDialog = isSystemDialog;
		this.title = title;
		this.singleChoiceItems = singleChoiceItems;
		this.checkedItem = checkedItem;
		mStyle = style;
	}

	/**
	 * 构造进度条对话框
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param style
	 * @param title
	 * @param progress
	 */
	public AlertDialogCreator(Context ctx, boolean isSystemDialog,
			DialogStyle style, String title, int progress) {
		this.mContext = ctx;
		this.isSystemDialog = isSystemDialog;
		this.title = title;
		mStyle = style;
		this.mProgress = progress;
	}

	/**
	 * 构造一个有两个按钮的普通对话框，包含内容有标题栏、消息栏、一个确定按钮， 一个取消按钮。标题栏和消息栏的缺省内容均为""。
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 *            true表示需要创建一个系统对话框，false表示创建自定义对话框
	 * @return 缺省对话框对象。请通过此实例构造所需要的对话框。
	 * @author Alonso Lee
	 */
	public static AlertDialogCreator createTwoButtonNormalDialog(Context ctx,
			boolean isSystemDialog) {
		return new AlertDialogCreator(ctx, isSystemDialog,
				DialogStyle.STYLE_TWO_BUTTON_DIALOG_NORMAL, "", "");
	}

	/**
	 * 构造一个带有一个按钮的普通对话框，包含内容有标题栏、消息栏、一个确定按钮。 标题栏和消息栏的缺省内容均为""的对话框。
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 *            true表示需要创建一个系统对话框，false表示创建自定义对话框
	 * @return 缺省对话框对象。请通过此实例构造所需要的对话框。
	 * @author Alonso Lee
	 */
	public static AlertDialogCreator createOneButtonNormalDialog(Context ctx,
			boolean isSystemDialog) {
		return new AlertDialogCreator(ctx, isSystemDialog,
				DialogStyle.STYLE_ONE_BUTTON_DIALOG_NORMAL, "", "");
	}

	/**
	 * 构造一个带有一个按钮的普通对话框，包含内容有标题栏、消息栏、一个确定按钮。 标题栏和消息栏的缺省内容均为"",并带有自定义view的对话框。
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 *            true表示需要创建一个系统对话框，false表示创建自定义对话框
	 * @param CustomView
	 *            自定义view
	 * @return 缺省对话框对象。请通过此实例构造所需要的对话框。
	 * @return
	 */
	public static AlertDialogCreator createOneButtonWithCustomViewDialog(
			Context ctx, boolean isSystemDialog, View CustomView) {
		return new AlertDialogCreator(ctx, isSystemDialog,
				DialogStyle.STYLE_ONE_BUTTON_DIALOG_WITH_CUSTOM_VIEW, "", "",
				CustomView);
	}

	/**
	 * 构造一个带有一个按钮的普通对话框，包含内容有标题栏、消息栏、一个确定按钮。 标题栏和消息栏的缺省内容均为"",并带有自定义view的对话框。
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 *            true表示需要创建一个系统对话框，false表示创建自定义对话框
	 * @param CustomView
	 *            自定义view
	 * @return 缺省对话框对象。请通过此实例构造所需要的对话框。
	 * @return
	 */
	public static AlertDialogCreator createTwoButtonWithCustomViewDialog(
			Context ctx, boolean isSystemDialog, View CustomView) {
		return new AlertDialogCreator(ctx, isSystemDialog,
				DialogStyle.STYLE_TWO_BUTTON_DIALOG_WITH_CUSTOM_VIEW, "", "",
				CustomView);
	}

	/**
	 * 构造一个带有list item的单按钮对话框，包含内容有标题栏、一个确定按钮。 标题栏的缺省内容均为""。
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param items
	 * @return
	 */
	public static AlertDialogCreator createNoButtonWithListItemDialog(
			Context ctx, boolean isSystemDialog, String[] items) {
		return new AlertDialogCreator(ctx, isSystemDialog,
				DialogStyle.STYLE_NO_BUTTON_DIALOG_WITH_LIST_ITEM, "", items);
	}

	/**
	 * 构造一个带有list item的单按钮对话框，包含内容有标题栏、一个确定按钮。 标题栏的缺省内容均为""。
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param items
	 * @return
	 */
	public static AlertDialogCreator createOneButtonWithListItemDialog(
			Context ctx, boolean isSystemDialog, String[] items) {

		return new AlertDialogCreator(ctx, isSystemDialog,
				DialogStyle.STYLE_ONE_BUTTON_DIALOG_WITH_LIST_ITEM, "", items);
	}

	/**
	 * 构造一个带有list item的双按钮对话框，包含内容有标题栏、一个确定按钮。 标题栏的缺省内容均为""。
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param items
	 * @return
	 */
	public static AlertDialogCreator createTwoButtonWithListItemDialog(
			Context ctx, boolean isSystemDialog, String[] items) {
		return new AlertDialogCreator(ctx, isSystemDialog,
				DialogStyle.STYLE_TWO_BUTTON_DIALOG_WITH_LIST_ITEM, "", items);
	}

	/**
	 * 构造一个带有单选列表的单按钮对话框，包含内容有标题栏、一个确定按钮
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param items
	 * @return
	 */
	public static AlertDialogCreator createOneButtonWithSingleChoiceListDialog(
			Context ctx, boolean isSystemDialog, String[] items, int checkedItem) {
		return new AlertDialogCreator(
				ctx,
				isSystemDialog,
				DialogStyle.STYLE_ONE_BUTTON_DIALOG_WITH_SINGLE_CHOICE_LIST_ITEM,
				"", items, checkedItem);
	}

	/**
	 * 构造一个带有单选列表的双按钮对话框，包含内容有标题栏、一个确定按钮、一个取消按钮
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param items
	 * @return
	 */
	public static AlertDialogCreator createTwoButtonWithSingleChoiceListDialog(
			Context ctx, boolean isSystemDialog, String[] items, int checkedItem) {
		return new AlertDialogCreator(
				ctx,
				isSystemDialog,
				DialogStyle.STYLE_TWO_BUTTON_DIALOG_WITH_SINGLE_CHOICE_LIST_ITEM,
				"", items, checkedItem);
	}

	/**
	 * 构造一个带有多选列表的双按钮对话框，包含内容有标题栏、一个确定按钮、一个取消按钮
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param items
	 * @return
	 */
	public static AlertDialogCreator createOneButtonWithMultiChoiceListDialog(
			Context ctx, boolean isSystemDialog, String[] items,
			boolean[] checkedItem) {
		return new AlertDialogCreator(
				ctx,
				isSystemDialog,
				DialogStyle.STYLE_ONE_BUTTON_DIALOG_WITH_MULTI_CHOICE_LIST_ITEM,
				"", items, checkedItem);
	}

	/**
	 * 构造一个带有多选列表的双按钮对话框，包含内容有标题栏、一个确定按钮、一个取消按钮
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param items
	 * @return
	 */
	public static AlertDialogCreator createTwoButtonWithMultiChoiceListDialog(
			Context ctx, boolean isSystemDialog, String[] items,
			boolean[] checkedItem) {
		return new AlertDialogCreator(
				ctx,
				isSystemDialog,
				DialogStyle.STYLE_TWO_BUTTON_DIALOG_WITH_MULTI_CHOICE_LIST_ITEM,
				"", items, checkedItem);
	}

	/**
	 * 构造一个带有进度条的单按钮对话框，包含内容有标题栏、一个确定按钮、一个进度条。
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param items
	 * @return
	 */
	public static AlertDialogCreator createOneButtonWithProgressBarDialog(
			Context ctx, boolean isSystemDialog, int progress) {
		return new AlertDialogCreator(ctx, isSystemDialog,
				DialogStyle.STYLE_ONE_BUTTON_DIALOG_WITH_PROGRESS_BAR, "",
				progress);
	}

	/**
	 * 构造一个带有进度条的单按钮对话框，包含内容有标题栏、一个确定按钮、一个进度条、一个取消按钮。
	 * 
	 * @param ctx
	 * @param isSystemDialog
	 * @param items
	 * @return
	 */
	public static AlertDialogCreator createTwoButtonWithProgressBarDialog(
			Context ctx, boolean isSystemDialog, int progress) {
		return new AlertDialogCreator(ctx, isSystemDialog,
				DialogStyle.STYLE_TWO_BUTTON_DIALOG_WITH_PROGRESS_BAR, "",
				progress);
	}

	/**
	 * 显示对话框
	 * 
	 * @author Alonso Lee
	 */
	public void showDialog() {

		if (dialog != null) {
			// 对话框已经生成，直接显示
			startShowDialog();
		} else {
			createDialog();
		}
	}

	/**
	 * 生成对话框并显示
	 */
	private void createDialog() {
		if (isSystemDialog) {
			// 生成系统对话框
			AlertDialog.Builder build = new AlertDialog.Builder(mContext);

			if (customContentView != null) {
				build.setView(customContentView);
			}

			if (singleChoiceItems != null) {
				build.setSingleChoiceItems(singleChoiceItems, checkedItem,
						singleChoiceListener);
			}

			// 监听back键按下事件
			build.setOnKeyListener(new OnKeyListener() {

				@Override
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					// TODO Auto-generated method stub
					if (keyCode == KeyEvent.KEYCODE_BACK) {
						// UI对话框已经消失了
						if (mAlertDialogEventsListener != null) {
							mAlertDialogEventsListener.onBackPressed();
						}
					}
					return false;
				}
			});

			build.setCancelable(isCancelable);

			if (isStringValid(title)) {
				build.setTitle(title);
			}

			if (isStringValid(message)) {
				build.setMessage(message);
			}

			if (mBitmap != null) {
				Drawable drawable = new BitmapDrawable(mBitmap);
				build.setIcon(drawable);
			}

			switch (mStyle) {
			case STYLE_ONE_BUTTON_DIALOG_NORMAL:
				// 单个按钮系统对话框
				createSystemDialogWithOneButtonNormal(build);
				break;
			case STYLE_TWO_BUTTON_DIALOG_NORMAL:
				// 两个按钮系统对话框
				createSystemDialogWithTwoButtonNormal(build);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_CUSTOM_VIEW:
				// 单个按钮带自定义view的系统对话框
				createSystemDialogWithOneButtonAndCustomView(build);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_CUSTOM_VIEW:
				// 两个按钮带自定义view的系统对话框
				createSystemDialogWithTwoButtonAndCustomView(build);
				break;
			case STYLE_NO_BUTTON_DIALOG_WITH_LIST_ITEM:
				// 系统列表对话框，不带按钮
				createSystemDialogWithNoButtonAndListItem(build);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_LIST_ITEM:
				// 带有list item的单按钮系统对话框
				createSystemDialogWithOneButtonAndListItem(build);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_LIST_ITEM:
				// 带有list item的双按钮系统对话框
				createSystemDialogWithTwoButtonAndListItem(build);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_SINGLE_CHOICE_LIST_ITEM:
				// 带有单选列表的单按钮系统对话框
				createSystmDialogWithOneButtonAndSingleChoiceList(build);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_SINGLE_CHOICE_LIST_ITEM:
				// 带有单选列表的双按钮系统对话框
				createSystmDialogWithTwoButtonAndSingleChoiceList(build);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_MULTI_CHOICE_LIST_ITEM:
				// 带有多选列表的单按钮系统对话框
				createSystemDialogWithOneButtonAndMultiChoiceList(build);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_MULTI_CHOICE_LIST_ITEM:
				// 带有多选列表的双按钮系统对话框
				createSystemDialogWithTwoButtonAndMultiChoiceList(build);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_PROGRESS_BAR:
				// 带有进度条的单按钮系统对话框
				createSystemDialogWithOneButtonAndProgressBar(build);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_PROGRESS_BAR:
				// 带有进度条的双按钮系统对话框
				createSystemDialogWithTwoButtonAndProgressBar(build);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_TEXT_ENTRY:
				// 带有文字输入框的单按钮系统对话框
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_TEXT_ENTRY:
				// 带有文字输入框的双按钮系统对话框
				break;
			}

			if ((mStyle != DialogStyle.STYLE_ONE_BUTTON_DIALOG_WITH_PROGRESS_BAR)
					&& (mStyle != DialogStyle.STYLE_TWO_BUTTON_DIALOG_WITH_PROGRESS_BAR)) {
				dialog = build.create();
			}
			dialog.setCanceledOnTouchOutside(false);

		} else {

			// 生成自定义对话框
			// dialog = new Dialog(mContext,
			// android.R.style.Theme_Dialog);
			dialog = new Dialog(mContext, R.style.Theme_ListDialog);
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			// 监听back键按下事件
			dialog.setOnKeyListener(new OnKeyListener() {

				@Override
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					// TODO Auto-generated method stub
					if (keyCode == KeyEvent.KEYCODE_BACK) {
						// UI对话框已经消失了
						if (mAlertDialogEventsListener != null) {
							mAlertDialogEventsListener.onBackPressed();
						}
					}
					return false;
				}
			});

			switch (mStyle) {
			case STYLE_ONE_BUTTON_DIALOG_NORMAL:
				// 单个按钮对话框
				createCustomDialogWithOneButtonNormal(dialog);
				break;
			case STYLE_TWO_BUTTON_DIALOG_NORMAL:
				// 两个按钮对话框
				createCustomDialogWithTwoButtonNormal(dialog);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_CUSTOM_VIEW:
				// 单个按钮带自定义view
				createCustomDialogWithOneButtonAndCustomView(dialog);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_CUSTOM_VIEW:
				// 两个按钮带自定义view
				createCustomDialogWithTwoButtonAndCustomView(dialog);
				break;
			case STYLE_NO_BUTTON_DIALOG_WITH_LIST_ITEM:
				// 无按钮的带有list item的单按钮自定义对话框
				createCustomNoButtonWithListItemDialog(dialog);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_LIST_ITEM:
				// 带有list item的单按钮自定义对话框
				createCustomOneButtonWithListItemDialog(dialog);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_LIST_ITEM:
				// 带有list item的单按钮自定义对话框
				createCustomTwoButtonWithListItemDialog(dialog);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_SINGLE_CHOICE_LIST_ITEM:
				// 带有单选列表的单按钮自定义对话框
				createCustomOneButtonWithSingleChoiceListDialog(dialog);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_SINGLE_CHOICE_LIST_ITEM:
				// 带有单选列表的双按钮自定义对话框
				createCustomTwoButtonWithSingleChoiceListDialog(dialog);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_MULTI_CHOICE_LIST_ITEM:
				// 带有多选列表的单按钮自定义对话框
				createCustomOneButtonWithMultiChoiceListDialog(dialog);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_MULTI_CHOICE_LIST_ITEM:
				// 带有多选列表的双按钮自定义对话框
				createCustomTwoButtonWithMultiChoiceListDialog(dialog);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_PROGRESS_BAR:
				// 带有进度条的单按钮自定义对话框
				createCustomOneButtonWithProgressBar(dialog);
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_PROGRESS_BAR:
				// 带有进度条的双按钮自定义对话框
				createCustomTwoButtonWithProgressBar(dialog);
				break;
			case STYLE_ONE_BUTTON_DIALOG_WITH_TEXT_ENTRY:
				// 带有文字输入框的单按钮自定义对话框
				break;
			case STYLE_TWO_BUTTON_DIALOG_WITH_TEXT_ENTRY:
				// 带有文字输入框的双按钮自定义对话框
				break;
			}
			// 构建其他组件
			createOtherComponent();
			dialog.setCancelable(this.isCancelable);
			// 外部界面点击不做响应
			dialog.setCanceledOnTouchOutside(false);
		}
		startShowDialog();

	}

	/**
	 * 销毁对话框
	 * 
	 * @author Alonso Lee
	 */
	public void dismissDialog() {
		// UI对话框已经消失了
		dialog.dismiss();
		if (mAlertDialogEventsListener != null) {
			// 回调通知监听这
			mAlertDialogEventsListener.onDismiss();
		}
	}

	/**
	 * 显示对话框
	 * 
	 * @author Alonso Lee
	 */
	public void startShowDialog() {
		// 开始显示对话框
		dialog.show();
		if (mAlertDialogEventsListener != null) {
			// 回调通知监听这
			mAlertDialogEventsListener.onShow();
		}
	}

	/**
	 * 设定对话框显示的icon
	 * 
	 * @param b
	 * @author Alonso Lee
	 */
	public void setIcon(Bitmap b) {
		mBitmap = b;
	}

	/**
	 * 设定此对话框是否为系统对话框
	 * 
	 * @param isSystemDialog
	 *            true 系统对话框
	 * @author Alonso Lee
	 */
	public void setSystemDialog(boolean isSystemDialog) {
		this.isSystemDialog = isSystemDialog;
	}

	/**
	 * 检测对话框是否为系统对话框
	 * 
	 * @return
	 * @author Alonso Lee
	 */
	public boolean isSystemDialog() {
		return this.isSystemDialog;
	}

	public void setCentralButtonName(String n) {
		this.centralButtonName = n;
	}

	/**
	 * 设定系统对话框左键点击事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setPostiveButtonListener(
			DialogInterface.OnClickListener listener) {
		this.postiveListener = listener;
	}

	/**
	 * 获取系统对话框左键点击事件监听器
	 * 
	 * @param listener
	 * @return
	 * @author Alonso Lee
	 */
	public DialogInterface.OnClickListener getPostiveButtonListener(
			DialogInterface.OnClickListener listener) {
		return this.postiveListener;
	}

	/**
	 * 设定系统对话框右键点击事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setNegativeButtonListener(
			DialogInterface.OnClickListener listener) {
		this.negativeListener = listener;
	}

	/**
	 * 获取系统对话框右键点击事件监听器
	 * 
	 * @param listener
	 * @return
	 * @author Alonso Lee
	 */
	public DialogInterface.OnClickListener getNegativeButtonListener(
			DialogInterface.OnClickListener listener) {
		return this.negativeListener;
	}

	/**
	 * 设定自定义对话框左键点击事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setLeftKeyListener(AlDialogOnClickListener listener) {
		mLeftKeyListener = listener;
	}

	/**
	 * 设定对话框右键点击事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setRightKeyListener(AlDialogOnClickListener listener) {
		mRightKeyListener = listener;
	}

	/**
	 * 设定对话框中间键点击事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setCenterKeyListener(AlDialogOnClickListener listener) {
		mCentralKeyListener = listener;
	}

	/**
	 * 设定列表对话框item项点击事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setItemClickListener(AlItemOnClickListener listener) {
		this.mItemClickListener = listener;
	}

	/**
	 * 设定单选对话框item项点击事件监听器
	 * 
	 * @param listener
	 */
	public void setSingleChoiceClickListener(AlItemOnClickListener listener) {
		this.mSingleChoiceListener = listener;
	}

	/**
	 * 设定多选对话框item项点击事件监听器
	 * 
	 * @param listener
	 */
	public void setMultiChoiceClickListener(
			AlMultiChoiceOnClickListener listener) {
		this.mMultiChoiceListener = listener;
	}

	/**
	 * 设置左键的selector
	 * 
	 * @param selector
	 * @author Alonso Lee
	 */
	public void setLeftKeySelector(int selector) {
		leftButtonSelector = selector;
	}

	/**
	 * 设置右键的selector
	 * 
	 * @param selector
	 * @author Alonso Lee
	 */
	public void setRightKeySelector(int selector) {
		rightButtonSelector = selector;
	}

	/**
	 * 设置中间键的selector
	 * 
	 * @param selector
	 * @author Alonso Lee
	 */
	public void setCentralKeySelector(int selector) {
		centralButtonSelector = selector;
	}

	/**
	 * 设置标题栏
	 * 
	 * @param title
	 * @author Alonso Lee
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * 设置显示内容
	 * 
	 * @param message
	 * @author Alonso Lee
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 设置左键名
	 * 
	 * @param name
	 * @author Alonso Lee
	 */
	public void setLeftButtonName(String name) {
		leftButtonName = name;
	}

	/**
	 * 设定右键名
	 * 
	 * @param name
	 * @author Alonso Lee
	 */
	public void setRightButtonName(String name) {
		rightButtonName = name;
	}

	/**
	 * 设置对话框事件监听器
	 * 
	 * @param listener
	 */
	public void setDialogEventsListener(AlertDialogEventListener listener) {
		mAlertDialogEventsListener = listener;
	}

	/**
	 * 设定对话框是否能被back键销毁
	 * 
	 * @param isCancelable
	 * @author Alonso Lee
	 */
	public void setCancelable(boolean isCancelable) {
		this.isCancelable = isCancelable;
	}

	public void setProgress(int progress) {
		if (progress > MAX_PROGRESS) {
			progress = MAX_PROGRESS;
		}
		this.mProgress = progress;
	}

	public int getProgress() {
		return this.mProgress;
	}

	public void setProgressBarIncrementStep(int step) {
		this.incrementStep = step;
	}

	public int getProgressBarIncrementStep() {
		return this.incrementStep;
	}

	public void updateProgress(int progress) {
		if (mProgressDialog != null) {
			mProgressDialog.incrementProgressBy(progress);
		}
	}

	public int getCurrentProgress() {
		if (mProgressDialog != null) {
			return mProgressDialog.getProgress();
		}
		return 0;
	}

	public void setCurrentProgressMessage(String message) {
		if (tvProgressMessage != null) {
			if (isStringValid(message)) {
				tvProgressMessage.setVisibility(View.VISIBLE);
				tvProgressMessage.setText(message);
			} else {
				tvProgressMessage.setVisibility(View.GONE);
			}
		}
	}

	public Dialog getAlertDialog() {
		return this.dialog;
	}

	/**************************************************** 以下为生成系统对话框的方法 **********************************************************************/
	/**
	 * 创建单个按钮的系统对话框
	 */
	private void createSystemDialogWithOneButtonNormal(AlertDialog.Builder build) {
		neutralListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if (mCentralKeyListener != null) {
					mCentralKeyListener.onClick();
				}
			}
		};

		// 如果设定了自定义的中间键名，那么直接设定进去
		if (isStringValid(centralButtonName)) {
			build.setPositiveButton(centralButtonName, neutralListener);
		} else {
			// 否则使用缺省名"确定"
			build.setPositiveButton(ALLocalEnv.getLocalEnv().getContext()
					.getString(R.string.BtnOk), neutralListener);
		}

	}

	/**
	 * 创建两个按钮的系统对话框
	 */
	private void createSystemDialogWithTwoButtonNormal(AlertDialog.Builder build) {

		postiveListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if (mLeftKeyListener != null) {
					mLeftKeyListener.onClick();
				}
			}
		};

		negativeListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if (mRightKeyListener != null) {
					mRightKeyListener.onClick();
				}
			}
		};

		if (isStringValid(leftButtonName)) {
			build.setPositiveButton(leftButtonName, postiveListener);
		} else {
			build.setPositiveButton(ALLocalEnv.getLocalEnv().getContext()
					.getString(R.string.BtnOk), postiveListener);
		}

		if (isStringValid(rightButtonName)) {
			build.setNegativeButton(leftButtonName, negativeListener);
		} else {
			build.setNegativeButton(ALLocalEnv.getLocalEnv().getContext()
					.getString(R.string.BtnCancel), negativeListener);
		}
	}

	/**
	 * 创建单按钮的带自定义view的系统对话框
	 * 
	 * @param build
	 */
	private void createSystemDialogWithOneButtonAndCustomView(
			AlertDialog.Builder build) {
		createSystemDialogWithOneButtonNormal(build);
		if (customContentView != null) {
			build.setView(customContentView);
		}
	}

	/**
	 * 创建双按钮的带自定义view的系统对话框
	 * 
	 * @param build
	 */
	private void createSystemDialogWithTwoButtonAndCustomView(
			AlertDialog.Builder build) {
		if (customContentView != null) {
			build.setView(customContentView);
		}
		createSystemDialogWithTwoButtonNormal(build);
	}

	/**
	 * 构造一个带有list item的单按钮系统对话框
	 * 
	 * @param build
	 */
	private void createSystemDialogWithOneButtonAndListItem(
			AlertDialog.Builder build) {
		createSystemDialogWithNoButtonAndListItem(build);
		createSystemDialogWithOneButtonNormal(build);
	}

	/**
	 * 创建无按钮，带列表的对话框
	 * 
	 * @param build
	 */
	private void createSystemDialogWithNoButtonAndListItem(
			AlertDialog.Builder build) {
		if (items != null) {
			itemListener = new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					if (mItemClickListener != null) {
						mItemClickListener.OnClick(which);
					}
				}
			};

			// 如果设置了Item
			build.setItems(items, itemListener);
		}
	}

	/**
	 * 构造一个带有list item的双按钮系统对话框
	 * 
	 * @param build
	 */
	private void createSystemDialogWithTwoButtonAndListItem(
			AlertDialog.Builder build) {

		if (items != null) {
			// 如果设置了Item
			itemListener = new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					if (mItemClickListener != null) {
						mItemClickListener.OnClick(which);
					}
				}
			};

			build.setItems(items, itemListener);
		}
		createSystemDialogWithTwoButtonNormal(build);
	}

	/**
	 * 构造一个单选带一个按钮的系统对话框
	 * 
	 * @param build
	 */
	private void createSystmDialogWithOneButtonAndSingleChoiceList(
			AlertDialog.Builder build) {

		if (singleChoiceItems != null) {
			// 如果设置了Item
			singleChoiceListener = new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					if (mSingleChoiceListener != null) {
						mSingleChoiceListener.OnClick(which);
					}
				}
			};

			build.setSingleChoiceItems(singleChoiceItems, checkedItem,
					singleChoiceListener);
		}

		createSystemDialogWithOneButtonNormal(build);
	}

	/**
	 * 构造一个多选带两个按钮的单选系统对话框
	 * 
	 * @param build
	 */
	private void createSystmDialogWithTwoButtonAndSingleChoiceList(
			AlertDialog.Builder build) {
		if (singleChoiceItems != null) {
			// 如果设置了Item
			singleChoiceListener = new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					if (mSingleChoiceListener != null) {
						mSingleChoiceListener.OnClick(which);
					}
				}
			};

			build.setSingleChoiceItems(singleChoiceItems, checkedItem,
					singleChoiceListener);
		}

		createSystemDialogWithTwoButtonNormal(build);
	}

	/**
	 * 创建单按钮，带多选列表的系统对话框
	 * 
	 * @param build
	 */
	private void createSystemDialogWithOneButtonAndMultiChoiceList(
			AlertDialog.Builder build) {
		if (multiChoiceItems != null) {
			// 如果设置了Item
			multiChoiceListener = new DialogInterface.OnMultiChoiceClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which,
						boolean isChecked) {
					if (mMultiChoiceListener != null) {
						mMultiChoiceListener.OnClick(which, isChecked);
					}
				}
			};
			// 如果传递进来的各item的缺省状态不为空，那么判断下是不是与item项数组长度一致
			if (multiCheckedItem != null) {
				if (multiCheckedItem.length != multiChoiceItems.length) {
					// 与item项数组长度不一致
					boolean[] temp = new boolean[multiChoiceItems.length];
					int len = 0;
					if (multiCheckedItem.length > multiChoiceItems.length) {
						// 如果传递来的状态数组长度大于item数组，那么只需要拷贝item数组长度的状态量就可以了
						len = temp.length;
					} else {
						// 否则，需要拷贝状态量数组中的所有数据
						len = multiCheckedItem.length;
					}

					for (int i = 0; i < len; i++) {
						temp[i] = multiCheckedItem[i];
					}
					multiCheckedItem = temp;
				}
			}
			build.setMultiChoiceItems(multiChoiceItems, multiCheckedItem,
					multiChoiceListener);
		}
		createSystemDialogWithOneButtonNormal(build);
	}

	/**
	 * 创建双按钮，带多选列表的系统对话框
	 * 
	 * @param build
	 */
	private void createSystemDialogWithTwoButtonAndMultiChoiceList(
			AlertDialog.Builder build) {
		if (multiChoiceItems != null) {
			// 如果设置了Item
			multiChoiceListener = new DialogInterface.OnMultiChoiceClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which,
						boolean isChecked) {
					if (mMultiChoiceListener != null) {
						mMultiChoiceListener.OnClick(which, isChecked);
					}
				}
			};
			// 如果传递进来的各item的缺省状态不为空，那么判断下是不是与item项数组长度一致
			if (multiCheckedItem != null) {
				if (multiCheckedItem.length != multiChoiceItems.length) {
					// 与item项数组长度不一致
					boolean[] temp = new boolean[multiChoiceItems.length];
					int len = 0;
					if (multiCheckedItem.length > multiChoiceItems.length) {
						// 如果传递来的状态数组长度大于item数组，那么只需要拷贝item数组长度的状态量就可以了
						len = temp.length;
					} else {
						// 否则，需要拷贝状态量数组中的所有数据
						len = multiCheckedItem.length;
					}

					for (int i = 0; i < len; i++) {
						temp[i] = multiCheckedItem[i];
					}
					multiCheckedItem = temp;
				}
			}
			build.setMultiChoiceItems(multiChoiceItems, multiCheckedItem,
					multiChoiceListener);
		}
		createSystemDialogWithTwoButtonNormal(build);
	}

	/**
	 * 创建一个单按钮，带进度条的系统对话框
	 * 
	 * @param build
	 */
	private void createSystemDialogWithOneButtonAndProgressBar(
			AlertDialog.Builder build) {
		mProgressDialog = new AlProgressDialog(mContext);
		if (title != null) {
			mProgressDialog.setTitle(title);
		}
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mProgressDialog.setMax(MAX_PROGRESS);
		mProgressDialog.incrementProgressBy(incrementStep);

		neutralListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if (mCentralKeyListener != null) {
					mCentralKeyListener.onClick();
				}
			}
		};

		String button = null;
		// 如果设定了自定义的中间键名，那么直接设定进去
		if (isStringValid(centralButtonName)) {
			button = centralButtonName;
		} else {
			// 否则使用缺省名"确定"
			button = ALLocalEnv.getLocalEnv().getContext()
					.getString(R.string.BtnOk);
		}

		mProgressDialog.setButton(button, neutralListener);

		dialog = mProgressDialog;
	}

	/**
	 * 创建一个双按钮，带进度条的系统对话框
	 * 
	 * @param build
	 */
	private void createSystemDialogWithTwoButtonAndProgressBar(
			AlertDialog.Builder build) {
		mProgressDialog = new AlProgressDialog(mContext);
		if (title != null) {
			mProgressDialog.setTitle(title);
		}
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mProgressDialog.setMax(MAX_PROGRESS);
		mProgressDialog.incrementProgressBy(incrementStep);

		postiveListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (mLeftKeyListener != null) {
					mLeftKeyListener.onClick();
				}
			}
		};

		negativeListener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				if (mRightKeyListener != null) {
					mRightKeyListener.onClick();
				}
			}
		};

		String buttonLeft = null;
		String buttonRight = null;
		if (isStringValid(leftButtonName)) {
			buttonLeft = leftButtonName;
		} else {
			// 否则使用缺省名"确定"
			buttonLeft = ALLocalEnv.getLocalEnv().getContext()
					.getString(R.string.BtnOk);
		}

		if (isStringValid(rightButtonName)) {
			buttonRight = rightButtonName;
		} else {
			// 否则使用缺省名"取消"
			buttonRight = ALLocalEnv.getLocalEnv().getContext()
					.getString(R.string.BtnCancel);
		}

		mProgressDialog.setButton(buttonLeft, postiveListener);
		mProgressDialog.setButton2(buttonRight, negativeListener);

		dialog = mProgressDialog;
	}

	/**************************************************** 以下为生成自定义对话框的方法 **********************************************************************/

	/**
	 * 构造一个普通的单按钮自定义对话框
	 */
	private void createCustomDialogWithOneButtonNormal(Dialog d) {
		d.setContentView(R.layout.dialog_normal);

		d.findViewById(R.id.one_button_layout).setVisibility(View.VISIBLE);
		mCustomCentralButton = new AlButton(
				(Button) d.findViewById(R.id.center_button),
				mCentralKeyListener);

		if (centralButtonSelector != -1) {
			mCustomCentralButton.mButton
					.setBackgroundResource(centralButtonSelector);
		}
		setDefaultListener(mCustomCentralButton);
	}

	/**
	 * 构造一个普通的双按钮自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomDialogWithTwoButtonNormal(Dialog d) {
		d.setContentView(R.layout.dialog_normal);
		mCustomleftButton = new AlButton(
				(Button) d.findViewById(R.id.left_button), mLeftKeyListener);
		mCustomRightButton = new AlButton(
				(Button) d.findViewById(R.id.right_button), mRightKeyListener);

		d.findViewById(R.id.two_button_layout).setVisibility(View.VISIBLE);

		if (leftButtonSelector != -1) {
			mCustomleftButton.mButton.setBackgroundResource(leftButtonSelector);
		}

		if (rightButtonSelector != -1) {
			mCustomRightButton.mButton
					.setBackgroundResource(leftButtonSelector);
		}
		// 左键设置一个缺省的按键监听器，点击直接关闭对话框
		setDefaultListener(mCustomleftButton);
		// 右键设置一个缺省的按键监听器，点击直接关闭对话框
		setDefaultListener(mCustomRightButton);
	}

	/**
	 * 构造一个普通的单按钮带view的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomDialogWithOneButtonAndCustomView(Dialog d) {
		d.setContentView(R.layout.dialog_with_custom_view);
		LinearLayout customLayout = (LinearLayout) d
				.findViewById(R.id.dialog_content_view);

		if (customContentView != null) {
			// customLayout属于已经属于dialog里的一个子view，想加入新东西，必须先remove
			// 掉
			// ((ViewGroup) customLayout.getParent()).removeView(customLayout);
			customLayout.removeAllViews();
			customLayout.addView(customContentView);
		}

		d.findViewById(R.id.one_button_layout).setVisibility(View.VISIBLE);
		mCustomCentralButton = new AlButton(
				(Button) d.findViewById(R.id.center_button),
				mCentralKeyListener);

		if (centralButtonSelector != -1) {
			mCustomCentralButton.mButton
					.setBackgroundResource(centralButtonSelector);
		}
		setDefaultListener(mCustomCentralButton);
	}

	/**
	 * 构造一个普通的双按钮带view的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomDialogWithTwoButtonAndCustomView(Dialog d) {
		d.setContentView(R.layout.dialog_with_custom_view);

		d.findViewById(R.id.two_button_layout).setVisibility(View.VISIBLE);

		mCustomleftButton = new AlButton(
				(Button) d.findViewById(R.id.left_button), mLeftKeyListener);
		mCustomRightButton = new AlButton(
				(Button) d.findViewById(R.id.right_button), mRightKeyListener);

		LinearLayout customLayout = (LinearLayout) d
				.findViewById(R.id.dialog_content_view);

		if (customContentView != null) {
			// customLayout属于已经属于dialog里的一个子view，想加入新东西，必须先remove
			// 掉
			customLayout.removeAllViews();
			customLayout.addView(customContentView);
		}

		if (leftButtonSelector != -1) {
			mCustomleftButton.mButton.setBackgroundResource(leftButtonSelector);
		}

		if (rightButtonSelector != -1) {
			mCustomRightButton.mButton
					.setBackgroundResource(leftButtonSelector);
		}

		setDefaultListener(mCustomleftButton);
		setDefaultListener(mCustomRightButton);
	}

	/**
	 * 构造一个无按钮，带列表的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomNoButtonWithListItemDialog(Dialog d) {
		d.setContentView(R.layout.dialog_with_list_item);
		dismissAllButton(d);
		ArrayList<CheckableItem> list = new ArrayList<CheckableItem>();
		CheckableItem item = null;
		for (String s : items) {
			if (s == null)
				continue;
			item = new CheckableItem();
			item.name = s;
			list.add(item);
		}

		isNeedshowBox = false;
		mListItemAdapter = new ListItemAdapter(list, false);

		mListView = (ListView) d.findViewById(R.id.center_listview);
		mListView.setCacheColorHint(0);
		mListView.setDivider(null);
		mListView.setAdapter(mListItemAdapter);
		cItemClickListener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {
				// TODO Auto-generated method stub
				if (mItemClickListener != null) {
					mItemClickListener.OnClick(position);
					// 选中就该消失了
					dismissDialog();
				}
			}
		};

		mListView.setOnItemClickListener(cItemClickListener);
	}

	/**
	 * 构造一个单按钮，带列表的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomOneButtonWithListItemDialog(Dialog d) {
		createCustomNoButtonWithListItemDialog(d);
		d.findViewById(R.id.one_button_layout).setVisibility(View.VISIBLE);
		mCustomCentralButton = new AlButton(
				(Button) dialog.findViewById(R.id.center_button),
				mCentralKeyListener);

		if (centralButtonSelector != -1) {
			mCustomCentralButton.mButton
					.setBackgroundResource(centralButtonSelector);
		}

		setDefaultListener(mCustomCentralButton);
	}

	/**
	 * 构造一个双按钮，带列表的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomTwoButtonWithListItemDialog(Dialog d) {
		createCustomNoButtonWithListItemDialog(d);
		d.findViewById(R.id.two_button_layout).setVisibility(View.VISIBLE);
		mCustomleftButton = new AlButton(
				(Button) dialog.findViewById(R.id.left_button),
				mLeftKeyListener);
		mCustomRightButton = new AlButton(
				(Button) dialog.findViewById(R.id.right_button),
				mRightKeyListener);

		if (leftButtonSelector != -1) {
			mCustomleftButton.mButton.setBackgroundResource(leftButtonSelector);
		}

		if (rightButtonSelector != -1) {
			mCustomRightButton.mButton
					.setBackgroundResource(leftButtonSelector);
		}

		setDefaultListener(mCustomleftButton);
		setDefaultListener(mCustomRightButton);
	}

	/**
	 * 构造一个无按钮，带单选列表的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomNoButtonWithSingleChoiceListDialog(Dialog d) {

		isSingleChoice = true;
		d.setContentView(R.layout.dialog_with_list_item);
		dismissAllButton(d);

		ArrayList<CheckableItem> list = new ArrayList<CheckableItem>();
		CheckableItem item = null;
		for (String s : singleChoiceItems) {
			item = new CheckableItem();
			item.name = s;
			list.add(item);
		}

		list.get(checkedItem).isChecked = true;

		isNeedshowBox = true;
		mListItemAdapter = new ListItemAdapter(list, isNeedshowBox);

		mListView = (ListView) d.findViewById(R.id.center_listview);
		mListView.setCacheColorHint(0);
		mListView.setAdapter(mListItemAdapter);
		cSingleChoickClickListener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {

				CheckableItem d = (CheckableItem) mListView.getAdapter()
						.getItem(position);

				if (d != null) {
					d.isChecked = true;
				}

				d = (CheckableItem) mListView.getAdapter().getItem(
						singleChoiceCurSelect);

				if (d != null) {
					d.isChecked = false;
				}

				mListItemAdapter.notifyDataSetChanged();

				if (mSingleChoiceListener != null) {
					mSingleChoiceListener.OnClick(position);
				}
				singleChoiceCurSelect = position;
			}
		};

		mListView.setOnItemClickListener(cSingleChoickClickListener);
	}

	/**
	 * 构造一个单按钮，带单选列表的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomOneButtonWithSingleChoiceListDialog(Dialog d) {
		createCustomNoButtonWithSingleChoiceListDialog(d);
		d.findViewById(R.id.one_button_layout).setVisibility(View.VISIBLE);
		mCustomCentralButton = new AlButton(
				(Button) dialog.findViewById(R.id.center_button),
				mCentralKeyListener);

		if (centralButtonSelector != -1) {
			mCustomCentralButton.mButton
					.setBackgroundResource(centralButtonSelector);
		}

		setDefaultListener(mCustomCentralButton);
	}

	/**
	 * 构造一个双按钮，带单选列表的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomTwoButtonWithSingleChoiceListDialog(Dialog d) {
		createCustomNoButtonWithSingleChoiceListDialog(d);
		d.findViewById(R.id.two_button_layout).setVisibility(View.VISIBLE);
		mCustomleftButton = new AlButton(
				(Button) dialog.findViewById(R.id.left_button),
				mLeftKeyListener);
		mCustomRightButton = new AlButton(
				(Button) dialog.findViewById(R.id.right_button),
				mRightKeyListener);

		if (leftButtonSelector != -1) {
			mCustomleftButton.mButton.setBackgroundResource(leftButtonSelector);
		}

		if (rightButtonSelector != -1) {
			mCustomRightButton.mButton
					.setBackgroundResource(leftButtonSelector);
		}

		setDefaultListener(mCustomleftButton);
		setDefaultListener(mCustomRightButton);
	}

	/**
	 * 构造一个无按钮，带多选列表的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomNoButtonWithMultiChoiceListDialog(Dialog d) {
		d.setContentView(R.layout.dialog_with_list_item);
		dismissAllButton(d);

		ArrayList<CheckableItem> list = new ArrayList<CheckableItem>();
		CheckableItem item = null;

		isNeedshowBox = true;

		// 如果传递进来的各item的缺省状态不为空，那么判断下是不是与item项数组长度一致
		if (multiCheckedItem != null) {
			if (multiCheckedItem.length != multiChoiceItems.length) {
				// 与item项数组长度不一致
				boolean[] temp = new boolean[multiChoiceItems.length];
				int len = 0;
				if (multiCheckedItem.length > multiChoiceItems.length) {
					// 如果传递来的状态数组长度大于item数组，那么只需要拷贝item数组长度的状态量就可以了
					len = temp.length;
				} else {
					// 否则，需要拷贝状态量数组中的所有数据
					len = multiCheckedItem.length;
				}

				for (int i = 0; i < len; i++) {
					temp[i] = multiCheckedItem[i];
				}
				multiCheckedItem = temp;
			}
		}

		if (multiChoiceItems.length > 0) {
			mMultiChoiceItems = new CheckableItem[multiChoiceItems.length];
			for (int i = 0; i < multiChoiceItems.length; i++) {
				item = new CheckableItem();
				item.name = multiChoiceItems[i];
				item.isChecked = multiCheckedItem[i];
				list.add(item);
			}
		}

		mListItemAdapter = new ListItemAdapter(list, isNeedshowBox);

		mListView = (ListView) d.findViewById(R.id.center_listview);
		mListView.setCacheColorHint(0);
		mListView.setAdapter(mListItemAdapter);

		cMultiChoiceClickListener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {
				// TODO Auto-generated method stub
				SparseBooleanArray a = mListView.getCheckedItemPositions();
				boolean isChecked = a.get(position);
				CheckableItem d = (CheckableItem) mListView.getAdapter()
						.getItem(position);
				if (d != null) {
					if (isChecked) {
						d.isChecked = true;
					} else {
						d.isChecked = false;
					}
					mListItemAdapter.notifyDataSetChanged();
				}

				if (mMultiChoiceListener != null) {
					mMultiChoiceListener.OnClick(position, isChecked);
				}
			}
		};

		mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		mListView.setOnItemClickListener(cMultiChoiceClickListener);
	}

	/**
	 * 构造一个单按钮，带多选列表的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomOneButtonWithMultiChoiceListDialog(Dialog d) {
		createCustomNoButtonWithMultiChoiceListDialog(d);
		createCustomOneButtonLayout(d);
	}

	/**
	 * 构造一个双按钮，带多选列表的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomTwoButtonWithMultiChoiceListDialog(Dialog d) {
		createCustomNoButtonWithMultiChoiceListDialog(d);
		createCustomTwoButtonLayout(d);
	}

	/**
	 * 构造一个无按钮，带进度条的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomNoButtonWithProgressBarDialog(Dialog d) {
		d.setContentView(R.layout.dialog_with_progressbar);
		dismissAllButton(d);
	}

	/**
	 * 构造一个单按钮，带进度条的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomOneButtonWithProgressBar(Dialog d) {
		createCustomNoButtonWithProgressBarDialog(d);
		createCustomOneButtonLayout(d);
	}

	/**
	 * 构造一个双按钮，带进度条的自定义对话框
	 * 
	 * @param d
	 */
	private void createCustomTwoButtonWithProgressBar(Dialog d) {
		createCustomNoButtonWithProgressBarDialog(d);
		createCustomTwoButtonLayout(d);
	}

	/**
	 * 配置自定义对话框的标题栏、消息栏等组件
	 */
	private void createOtherComponent() {
		// 标题栏
		tvtitle = (TextView) dialog.findViewById(R.id.dialog_title);

		if (tvtitle != null) {
			// 设置标题
			if (this.title != null && this.title.length() > 0) {
				tvtitle.setText(this.title);
			} else {
				tvtitle.setVisibility(View.GONE);
			}
		}
//		 强制设为不现实title
//		tvtitle.setVisibility(View.GONE);

		tvProgressMessage = (TextView) dialog
				.findViewById(R.id.progress_message);

		if (tvProgressMessage != null) {
			if (isStringValid(progressMessage)) {
				tvProgressMessage.setText(progressMessage);
			} else {
				tvProgressMessage.setVisibility(View.GONE);
			}
		}

		tvmsg = (TextView) dialog.findViewById(R.id.dialog_message);

		if (tvmsg != null) {
			if (this.message != null && this.message.length() > 0) {
				// 设置对话框信息
				tvmsg.setText(this.message);
			} else {
				tvmsg.setVisibility(View.GONE);
			}
		}

		if (mCustomleftButton != null && mCustomleftButton.mButton != null
				&& !TextUtils.isEmpty(leftButtonName)) {
			mCustomleftButton.mButton.setText(leftButtonName);
		}

		if (mCustomRightButton != null && mCustomRightButton.mButton != null
				&& !TextUtils.isEmpty(rightButtonName)) {
			mCustomRightButton.mButton.setText(rightButtonName);
		}

		if (mCustomCentralButton != null
				&& mCustomCentralButton.mButton != null
				&& !TextUtils.isEmpty(centralButtonName)) {
			mCustomCentralButton.mButton.setText(centralButtonName);
		}

		// 有图标，那么现实这个icon
		if (isNeedIcon) {

			if (null != mIcon) {
				mIcon.setVisibility(View.VISIBLE);
				mIcon.setImageBitmap(mBitmap);
			}
		}
	}

	/**
	 * 创建自定义对话框的单按钮布局
	 * 
	 * @param d
	 */
	private void createCustomOneButtonLayout(Dialog d) {
		d.findViewById(R.id.one_button_layout).setVisibility(View.VISIBLE);
		mCustomCentralButton = new AlButton(
				(Button) dialog.findViewById(R.id.center_button),
				mCentralKeyListener);

		if (centralButtonSelector != -1) {
			mCustomCentralButton.mButton
					.setBackgroundResource(centralButtonSelector);
		}

		setDefaultListener(mCustomCentralButton);
	}

	/**
	 * 创建自定义对话框的双按钮布局
	 * 
	 * @param d
	 */
	private void createCustomTwoButtonLayout(Dialog d) {
		d.findViewById(R.id.two_button_layout).setVisibility(View.VISIBLE);
		mCustomleftButton = new AlButton(
				(Button) dialog.findViewById(R.id.left_button),
				mLeftKeyListener);
		mCustomRightButton = new AlButton(
				(Button) dialog.findViewById(R.id.right_button),
				mRightKeyListener);

		if (leftButtonSelector != -1) {
			mCustomleftButton.mButton.setBackgroundResource(leftButtonSelector);
		}

		if (rightButtonSelector != -1) {
			mCustomRightButton.mButton
					.setBackgroundResource(leftButtonSelector);
		}

		setDefaultListener(mCustomleftButton);
		setDefaultListener(mCustomRightButton);
	}

	/**
	 * 对自定义对话框指定button设置一个缺省的点击事件监听器， 当点击事件到来时
	 * 
	 * @param button
	 */
	private void setDefaultListener(final AlButton button) {
		if (button == null) {
			return;
		}

		button.mButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismissDialog();
				if (button.mListener != null) {
					button.mListener.onClick();
				}
			}
		});
	}

	/**
	 * 此内部类用于自定义的列表、单选、多选对话框
	 * 
	 * @author alonso lee
	 * 
	 */
	private class ListItemAdapter extends BaseAdapter {

		private ArrayList<CheckableItem> list;
		private boolean showBox = false;

		public ListItemAdapter(ArrayList<CheckableItem> list, boolean showBox) {
			// TODO Auto-generated constructor stub
			this.list = list;
			this.showBox = showBox;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View view, ViewGroup arg2) {
			// TODO Auto-generated method stub
			DialogListItem mItem = null;

			if (view == null) {
				mItem = new DialogListItem(ALLocalEnv.getLocalEnv()
						.getContext());
			} else {
				mItem = (DialogListItem) view;
			}

			if (list.size() == 1) {
				mItem.setBackgroundResource(R.drawable.background_rounded_single);
			} else if (position == 0) {
				mItem.setBackgroundResource(R.drawable.background_rounded_top);
			} else if (position == list.size() - 1) {
				mItem.setBackgroundResource(R.drawable.background_rounded_bottom);
			} else {
				mItem.setBackgroundResource(R.drawable.background_rounded_middle);
			}

			mItem.mItemName.setText(list.get(position).name);

			if (showBox) {
				mItem.mItemBox.setVisibility(View.VISIBLE);
				// 多选
				if (!isSingleChoice) {
					if (list.get(position).isChecked) {
						mItem.mItemBox
								.setButtonDrawable(R.drawable.multi_choice_on);
					} else {
						mItem.mItemBox
								.setButtonDrawable(R.drawable.multi_choice_off);
					}
				} else {
					// 单选
					if (list.get(position).isChecked) {
						mItem.mItemBox
								.setButtonDrawable(R.drawable.single_choice_on);
					} else {
						mItem.mItemBox
								.setButtonDrawable(R.drawable.single_choice_off);
					}
				}

			} else {
				mItem.mItemBox.setVisibility(View.GONE);
			}

			return mItem;
		}
	}

	private class CheckableItem {

		public String name;
		public boolean isChecked;

		public CheckableItem() {
		}

	}

	/**
	 * 此类用于生成自定义的带进度条的对话框,重写 了OnBackPressed方法，通知UI。
	 * 
	 * @author Alonso lee
	 * 
	 */
	private class AlProgressDialog extends ProgressDialog {
		public AlProgressDialog(Context context) {
			super(context);
		}

		@Override
		public void onBackPressed() {
			progressBarOnBackPressed();
			super.onBackPressed();
		}
	}

	/**
	 * 通知UI，进度条对话框被按下back按键
	 */
	private void progressBarOnBackPressed() {
		if (mAlertDialogEventsListener != null) {
			mAlertDialogEventsListener.onBackPressed();
		}
	}

	/**
	 * 隐藏所有自定义按钮
	 * 
	 * @param d
	 */
	private void dismissAllButton(Dialog d) {
		d.findViewById(R.id.one_button_layout).setVisibility(View.GONE);
		d.findViewById(R.id.two_button_layout).setVisibility(View.GONE);
	}

	/**
	 * 此方法用于测试字符串是否有效
	 * 
	 * @param s
	 * @return
	 */
	private boolean isStringValid(String s) {
		if ((s != null) && (s.length() > 0)) {
			return true;
		}
		return false;
	}

}
